<!DOCTYPE html>
<html lang="zh-Hans">
  <head>
    
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>


<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">



  <meta name="description" content="和朋友写一个 php 项目, 发现当设置的 session_name 包含 . 的时候,用户的登录状态无法保存了.具体是什么原因导致的呢?"/>




  <meta name="keywords" content="技术, php, session, 杂鱼君的博客" />










  <link rel="alternate" href="/atom.xml" title="杂鱼君的博客">




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=2.6.0" />



<link rel="canonical" href="http://notes.wen.moe/post/php-session-bug.html"/>


<link rel="stylesheet" type="text/css" href="/css/style.css?v=2.6.0" />



  <link rel="stylesheet" type="text/css" href="/lib/fancybox/jquery.fancybox.css" />




  



  <script id="baidu_push">
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>









    <title> php 中 session_name 不能有句号的问题 - 杂鱼君的博客 </title>
  </head>

  <body><div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/." class="logo">杂鱼君的博客</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>

<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    
      <a href="/">
        <li class="mobile-menu-item">
          
          
            Home
          
        </li>
      </a>
    
      <a href="/archives/">
        <li class="mobile-menu-item">
          
          
            Archives
          
        </li>
      </a>
    
      <a href="/tags">
        <li class="mobile-menu-item">
          
          
            Tags
          
        </li>
      </a>
    
      <a href="/about">
        <li class="mobile-menu-item">
          
          
            About
          
        </li>
      </a>
    
  </ul>
</nav>

    <div class="container" id="mobile-panel">
      <header id="header" class="header"><div class="logo-wrapper">
  <a href="/." class="logo">杂鱼君的博客</a>
</div>

<nav class="site-navbar">
  
    <ul id="menu" class="menu">
      
        <li class="menu-item">
          <a class="menu-item-link" href="/">
            
            
              Home
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/archives/">
            
            
              Archives
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/tags">
            
            
              Tags
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/about">
            
            
              About
            
          </a>
        </li>
      
    </ul>
  
</nav>

      </header>

      <main id="main" class="main">
        <div class="content-wrapper">
          <div id="content" class="content">
            
  
  <article class="post">
    <header class="post-header">
      <h1 class="post-title">
        
          php 中 session_name 不能有句号的问题
        
      </h1>

      <div class="post-meta">
        <span class="post-time">
          2016-10-12
        </span>
        
        
      </div>
    </header>

    
    

    <div class="post-content">
      
        <p>在 php 的官方文档中,关于<code>session_name</code>设置的问题有如下的描述:</p>
<blockquote>
<p>会话名称至少需要一个字母，不能全部都使用数字， 否则，每次都会生成一个新的会话 ID</p>
</blockquote>
<p>这个似乎和我们的问题没什么关系,而下面有一条这样的描述:</p>
<blockquote>
<p>if you try to name a php session “example.com” it gets converted to “example_com” and everything breaks.</p>
</blockquote>
<p>他告诉我们<code>session_name</code>如果有<code>.</code>的话,他会将他转换成<code>_</code>,具体的过程却没有细说.所以我们还需要亲自动手.</p>
<p>首先我们设置<code>session_name</code>为<code>user.test</code>,然后输出<code>session_id</code>,代码如下:</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">session_name(<span class="string">'user.test'</span>);</div><div class="line">session_start();</div><div class="line"></div><div class="line"><span class="keyword">echo</span> session_id();</div></pre></td></tr></table></figure>
<p>不停的刷新页面的话,你会发下你每次输出的<code>session_id</code>是不同的.</p>
<p>打开 chrome 的开发者工具,查看本地的<code>cookie</code>的话,你会发现这样子:</p>
<p><img src="http://7xkdfx.com1.z0.glb.clouddn.com/%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202016-10-12%2022.39.59.png" alt="image"></p>
<p><code>cookie</code>中保存的<code>session_name</code>依然是<code>user.test</code>!那么到底是哪里出问题了呢?我们继续找:</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">session_name(<span class="string">'user.test'</span>);</div><div class="line">session_start();</div><div class="line"></div><div class="line">print_r($_COOKIE);</div><div class="line"><span class="keyword">echo</span> session_id();</div></pre></td></tr></table></figure>
<p>打印出来的结构如图:</p>
<p><img src="http://7xkdfx.com1.z0.glb.clouddn.com/%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202016-10-12%2022.48.23.png" alt="image"></p>
<p>问题出现在这里,php 在读取浏览器传过来的<code>cookie</code>中的<code>session_name</code>时,将<code>session_name</code>中的<code>.</code>转换成了<code>_</code>,具体流程如图:</p>
<p><img src="http://7xkdfx.com1.z0.glb.clouddn.com/session%E8%AF%BB%E5%8F%96%E6%B5%81%E7%A8%8B%E5%9B%BE.png" alt="image"></p>
<p>至于为什么读取<code>COOKIE</code>的时候会将<code>.</code> <code>_</code>呢,这就需要我们慢慢研究了.</p>

      
    </div>

    
      
      

  <div class="post-copyright">
    <p class="copyright-item">
      <span>Author: </span>
      <a href="http://notes.wen.moe">zakokun</a>
    </p>
    <p class="copyright-item">
      <span>Link: </span>
      <a href="http://notes.wen.moe/post/php-session-bug.html">http://notes.wen.moe/post/php-session-bug.html</a>
    </p>
    <p class="copyright-item">
      <span>License: </span>
      
      <a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/" target="_blank">知识共享署名-非商业性使用 4.0 国际许可协议</a>
    </p>
  </div>



      
      
    

    
      <footer class="post-footer">
        
          <div class="post-tags">
            
              <a href="/tags/技术/">技术</a>
            
              <a href="/tags/php/">php</a>
            
              <a href="/tags/session/">session</a>
            
          </div>
        
        
        
  <nav class="post-nav">
    
      <a class="prev" href="/post/2016-year-person-summarize.html">
        <i class="iconfont icon-left"></i>
        <span class="prev-text nav-default">2016年年终总结</span>
        <span class="prev-text nav-mobile">Prev</span>
      </a>
    
    
      <a class="next" href="/post/golang-time-package.html">
        <span class="next-text nav-default">phper 学 golang --日期时间处理</span>
        <span class="prev-text nav-mobile">Next</span>
        <i class="iconfont icon-right"></i>
      </a>
    
  </nav>

      </footer>
    

  </article>


          </div>
          
  <div class="comments" id="comments">
    
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    
  </div>


        </div>
      </main>

      <footer id="footer" class="footer">

  <div class="social-links">
    
      
        
          <a href="mailto:me@wen.moe" class="iconfont icon-email" title="email"></a>
        
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
        
          <a href="https://www.instagram.com/zakokun" class="iconfont icon-instagram" title="instagram"></a>
        
      
    
    
    
  </div>


<div class="copyright">
  <span class="power-by">
    Powered by <a class="hexo-link" href="https://hexo.io/">Hexo</a>
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    Theme -
    <a class="theme-link" href="https://github.com/ahonn/hexo-theme-even">Even</a>
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    Hosted by <a class="theme-link" href="https://pages.coding.me" style="font-weight: bold">Coding Pages</a>
  </span>

  <span class="copyright-year">
    
    &copy;
    
      2014 -
    
    2017

    <span class="heart">
      <i class="iconfont icon-heart"></i>
    </span>
    <span class="author">zakokun</span>
  </span>
</div>

      </footer>

      <div class="back-to-top" id="back-to-top">
        <i class="iconfont icon-up"></i>
      </div>
    </div>

    
  
  <script type="text/javascript">
    var disqus_config = function () {
        this.page.url = 'http://notes.wen.moe/post/php-session-bug.html';
        this.page.identifier = 'post/php-session-bug.html';
        this.page.title = 'php 中 session_name 不能有句号的问题';
    };
    (function() {
    var d = document, s = d.createElement('script');

    s.src = '//evanszhang.disqus.com/embed.js';

    s.setAttribute('data-timestamp', +new Date());
    (d.head || d.body).appendChild(s);
    })();  
  </script>




    
  





  
    <script type="text/javascript" src="/lib/jquery/jquery-3.1.1.min.js"></script>
  

  
    <script type="text/javascript" src="/lib/slideout/slideout.js"></script>
  

  
    <script type="text/javascript" src="/lib/fancybox/jquery.fancybox.pack.js"></script>
  


    <script type="text/javascript" src="/js/src/even.js?v=2.6.0"></script>
<script type="text/javascript" src="/js/src/bootstrap.js?v=2.6.0"></script>

  </body>
</html>
